<html>
	<head>
	<script src="webScrapper.js"></script>
	<script src="jquery-1.6.2.min.js"></script>
	<script src="findFile.js"></script>
	</head>
	<body onload="main()">
	</body>

<script type="text/javascript"> 
var count = 0;
main = function()
	{
	main.pateintArr = new Object();
	//main.filesArr
	var REF_TAB1 = 'http://tcga-data.nci.nih.gov/tcgafiles/ftp_auth/distro_ftpusers/anonymous/tumor/ov/bcr/minbiotab/clin/clinical_patient_public_ov.txt';
	webScrapper.getURL(REF_TAB1, saveTable1);
	return false;
	}

saveTable1 = function(pageObject)
	{
	//var REF_TAB2 = 'http://tcga-data.nci.nih.gov/tcgafiles/ftp_auth/distro_ftpusers/anonymous/tumor/ov/cgcc/broad.mit.edu/ht_hg-u133a/transcriptome/broad.mit.edu_OV.HT_HG-U133A.mage-tab.1.1004.0/broad.mit.edu_OV.HT_HG-U133A.sdrf.txt';
	var REF_TAB2 = 'http://tcga-data.nci.nih.gov/tcgafiles/ftp_auth/distro_ftpusers/anonymous/tumor/ov/cgcc/broad.mit.edu/ht_hg-u133a/transcriptome/broad.mit.edu_OV.HT_HG-U133A.mage-tab.1.1007.0/broad.mit.edu_OV.HT_HG-U133A.sdrf.txt';
	var header = pageObject.content.shift();
	header=header.split('\t');
	for( var i in pageObject.content )
		{
		if( !pageObject.content[i] ){continue;}
		var lineArr = pageObject.content[i].split('\t');
		main.pateintArr[lineArr[0]]=new Object();
		main.pateintArr[lineArr[0]].pateintID=lineArr[0];
		main.pateintArr[lineArr[0]].clinicalInfo = new Object();
		for( var j in lineArr )
			{
			main.pateintArr[lineArr[0]].clinicalInfo[header[j]]=lineArr[j];
			}
		}
	//console.log(main.pateintArr);
	webScrapper.getURL(REF_TAB2, saveTable2);
	return false;
	}

saveTable2 = function(pageObject)
	{
	var header = pageObject.content.shift();
	header=header.split('\t');
	for( var i in pageObject.content )
		{
		if( !pageObject.content[i] ){continue;}
		var lineArr = pageObject.content[i].split('\t');
		var pateintID = lineArr[0].slice(0,12);
		addData( pateintID, lineArr, header );
		}
	return false;
	}
var count = 0;
addData = function( patID, line, header )
	{
	if(count>2) {return false; };
	var FILE_IND = 'http://tcga-data.nci.nih.gov/tcgafiles/ftp_auth/distro_ftpusers/anonymous/tumor/ov/cgcc/broad.mit.edu/ht_hg-u133a/transcriptome/';
	if( main.pateintArr[patID] == null )
		{
		main.pateintArr[patID]=new Object;
		}
	main.pateintArr[patID].refData = new Object;
	for( var j in line )
		{
		if( line[j] == '->' ) { line[j] = 'undefined'; }
		main.pateintArr[patID].refData[header[j]]=line[j];
		}
	var FILE_KEY = 'Derived Array Data Matrix File';
	var filename = main.pateintArr[patID].refData[FILE_KEY];
	if( filename != 'undefined' )
		{
        //findFile( FILE_IND, filename, function(url){ main.addExpressionData(url,patID) } );
		}
	}
main.addExpressionData = function( url, patID )
	{
	
	main.pateintArr[patID].expressionData = new Object();
	count++;
	console.log("Sending found url" + count);
	webScrapper.getURL( url, main.addExpressionData.addData, patID );
	
	}

main.addExpressionData.addData = function( pageObject, patID )
	{
	console.log("Got here too\n!!");
	pageObject.content.shift();
	var header = pageObject.content.shift();
	for( var i in pageObject.content )
		{
		if( !pageObject.content[i] ) {continue;}
		var line = pageObject.content[i].split('\t');
		main.pateintArr[patID].expressionData[line[0]]=line[1];
		}
	console.log(main.pateintArr[patID]);
	}
	
</script>
</html>